import { Application } from "../Application";
import { ISession } from "../util/defines";

/**
 * 会话
 */
export abstract class Session implements ISession {

    app: Application;
    uid: number;
    sid: string;
    setting: { [key: string | number]: any } = {};
    /** 本地键值对数据 不会传递给其它服务器 */
    settingLocal: { [key: string | number]: any } = {};

    constructor(app: Application, sid: string) {
        this.app = app;
        this.sid = sid;
    }

    get(key: string | number) {
        return this.setting[key];
    }

    set(setting: { [key: string | number]: any }) {
        for (let key in setting) {
            this.setting[key] = setting[key];
        }
    }

    delete(...keys: (string | number)[]) {
        for (let key of keys) {
            delete this.settingLocal[key];
        }
    }

    getLocal(key: string | number) {
        return this.settingLocal[key];
    }

    setLocal(setting: { [key: string | number]: any }) {
        for (let key in setting) {
            this.settingLocal[key] = setting[key];
        }
    }

    deleteLocal(...keys: (string | number)[]) {
        for (let key of keys) {
            delete this.settingLocal[key];
        }
    }

    /**
     * 发送消息给客户端
     * @param msgId 
     * @param msg 
     * @param end 
     */
    abstract send(msgId: number, msg?: any, end?: boolean): void;

    /**
     * 发送多条消息给客户端
     * @param msgs 
     * @param end 
     */
    abstract sends(msgs: { msgId: number, msg?: any }[], end?: boolean): void;

}